<?php

namespace App\Service\Friend;

// use App\Constants\ErrorCode;

use App\Constants\ErrorCode;
use App\Model\UserProfile;
use App\Service\BaseService;

/**
 * 推荐用户卡片列表(上线提示跳转)
 */
class RecUserListService extends BaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        $page = $this->request->input('page', 1);
        $userProfile = UserProfile::getProfile($userId);
        $ret['list'] = [];
        //获取一批用户
        $showUserIds = UserProfile::getNewUsers(UserProfile::ROLE_USER, 1 - $userProfile->gender);
        $start = ($page - 1) * $this->pageNum;
        $showUserIds = array_slice($showUserIds, $start, $this->pageNum);

        if (empty($showUserIds)) {
            return $this->setError(ErrorCode::NO_VALID_HOST, "暂未发现适合你的人，请稍后再试~");
        }

        $usersInfo = UserProfile::getUsersProfile($showUserIds);
        array_walk($usersInfo, function ($userProfile) use (&$ret) {
            $profile = UserProfile::buildUserItem($userProfile, [], 400);
            $ret['list'][] = [
                'user_id' => $profile['user_id'],
                'avatar' => $profile['avatar'],
                'nick_name' => $profile['nick_name'],
                'age' => $profile['age'],
                'gender' => $profile['gender'],
                'online_status' => $profile['online_status'],
                'match_percent' => '与你缘分匹配度' . mt_rand(60, 99) . '%',
                'tip' => "{$profile['nick_name']}刚刚上线了~"
            ];
        });
        return $ret;
    }
}
